package com.platform.zuul.filter;

import com.apache.api.vo.ResultEntity;
import com.apache.jwt.Claims;
import com.apache.jwt.PostHttps;
import com.apache.passport.common.PassportHelper;
import com.apache.passport.common.XmlWhiteUtils;
import com.apache.passport.jwt.JwtUtil;
import com.apache.rpc.common.LoadRpcService;
import com.apache.rpc.entity.InterfaceRegister;
import com.apache.tools.StrUtil;
import com.apache.uct.common.LoginUser;
import com.apache.uct.common.PBOSSOTools;
import com.apache.uct.common.ToolsUtil;
import com.google.common.collect.Maps;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.platform.zuul.route.CustomRouteLocator;
import net.sf.json.JSONObject;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;
import java.util.concurrent.atomic.LongAdder;

/**
 * 请求权限验证filter
 */
@Component
public abstract class ReqPermissionFilter extends ZuulFilter {
	private Logger log = Logger.getLogger(getClass());
	private final String SSO_SERVICEID = "SSO-SERVER";
	private final String UCT_SERVICEID = "UCT-SERVER";
	@Autowired
	protected Environment env;

	@Autowired
	protected CustomRouteLocator routeLocator;
	protected static Map<String, Map<String,LongAdder>> hystrix = Maps.newConcurrentMap();

	public String runPermission(String serviceId) {
		RequestContext currentContext = RequestContext.getCurrentContext();
		HttpServletRequest request = currentContext.getRequest();
		HttpServletResponse response = currentContext.getResponse();
		String uri = request.getRequestURI();//获取uri信息
		//获取token
		String tokenCookie = PassportHelper.getInstance().getCurrCookie(request);
		//如果客户端根据token访问获取
		String pkt = StrUtil.doNull(request.getParameter("tokenId"),request.getHeader("tokenId"));
		if ("undefined".equalsIgnoreCase(pkt))
			pkt = "";
		String tokenId = StrUtil.doNull(tokenCookie,pkt);
		boolean tokenMark = false;
		String sysEname = "";
		String path ="/";
		if(StrUtil.isNotNull(serviceId)){
			sysEname = serviceId.split("-")[0].toLowerCase();
			path ="/"+sysEname;
		}
		if (!uri.equals(path + "/logout") && !uri.equals(path + "/cset")) {
			String code = request.getParameter("code");
			//是否用jwt验证
			if ("T".equals(ToolsUtil.getInstance().getValueByKey("if_jwt_check"))) {
				if (StrUtil.isNotNull(code)) {
					tokenId = getAccessToken(code, request, response);
				}
			}
			if (StrUtil.isNotNull(tokenId)) {
				tokenMark = auditTokenAndSso(tokenId, request, response);
			}
		}
		String defSso = "sso";
		String loginUrl = "";
		if (tokenMark) {
			if(StrUtil.isNotNull(tokenId)){
				String value = PassportHelper.getInstance().getCurrCookie(request, "_uc.sso");
				currentContext.addZuulRequestHeader("zuulucsso", value);
				currentContext.addZuulRequestHeader("zuultokenid", tokenId);
			}
		} else {
			String config_custom_login = env.getProperty(serviceId.toLowerCase()+".loginUrl");//配置文件自定义登录地址
			String cacheRouteLoginUrl  = routeLocator.getCacheRouteLoginUrl(serviceId.toLowerCase());
//			log.info("cacheRouteLoginUrl->"+cacheRouteLoginUrl);
			config_custom_login = StrUtil.doNull(config_custom_login,cacheRouteLoginUrl);//配置文件未配置时获取【网关路由配置】缓存设置的loginUrl
			if(StrUtil.isNull(config_custom_login)){
				try {
					loginUrl = getLonginUrl(request, response, tokenId,defSso);
				} catch (UnsupportedEncodingException e) {
				}
			} else {
				StringBuilder csetUrl = new StringBuilder(request.getScheme() + "://");
				csetUrl.append(request.getServerName());
				csetUrl.append(":");
				csetUrl.append("80".equals(request.getServerPort()) ? "" : request.getServerPort());
				loginUrl = csetUrl.toString() + config_custom_login;//自定义登录页面
			}
		}
		//add 20190506 解决白名单中无需配置项目contextPath前缀
		String env_path = StrUtil.doNull(env.getProperty("zuul.routes."+serviceId.toLowerCase()+".path"),routeLocator.getPath(serviceId.toLowerCase()));//路由配置的请求前缀context
		if(StrUtil.isNotNull(env_path)) {
			String contextPrefix = env_path.indexOf("/**") > -1 ? env_path.split("/\\*\\*")[0] : env_path;
			if (uri.indexOf(contextPrefix) > -1) {
				uri = uri.replace(contextPrefix, "");
			}
			if(StrUtil.isNull(sysEname)){
				sysEname = env_path.substring(1);
			}
			log.info("contextPrefix->"+contextPrefix+",uri="+uri);
		}

		if (request.getRequestURI().equals(path + "/logout")) {
			return doLogout(request, response, tokenId, loginUrl,defSso,currentContext);
		} else if (request.getRequestURI().equals(path + "/cset")) {
			return setCookie(request, response);//设置cookie
		} else {
			if (tokenMark) {
				return null;
			} else {
				if (!StrUtil.isNull(tokenId)) {
					clearCookie(tokenId, request, response, "/");
				}
				boolean mark = whitePathFiter(uri, request,sysEname);//白名单处理
				log.info("uri="+uri+" -> whitePathFiter = "+mark);
				if (!mark) {
					log.info("redirect loginUrl->"+loginUrl);
					return loginUrl;
				}
			}
		}
		return null;
	}

	/**
	 * description: 白名单验证
	 */
	protected boolean whitePathFiter(String uri, HttpServletRequest req,String sysCode) {
		//初始化白名单
		sysCode = StrUtil.isNotNull(sysCode) ? sysCode : "";
		String fileName = (StrUtil.isNull(sysCode)? "" : sysCode+"_") + "white.xml";
		boolean cacheFlag = XmlWhiteUtils.getInstance().getConfigForCache("", fileName);
		if(!cacheFlag){
			sysCode = "";
		}
		Map<String, ArrayList<String>> whiteMap = new HashMap();
		String path = req.getServletPath();
		log.info("whitePathFiter.sysCode->"+sysCode+" file ["+fileName+"]");
		XmlWhiteUtils.getInstance().deWhiteXml(whiteMap, sysCode);
		String whiteUrl = "/error.action,/errorPage,";
		/**白名单处理*/
		if (StrUtil.isNotNull(whiteUrl)) {
			String[] wus = whiteUrl.split(",");
			for (int i = 0; i < wus.length; i++) {
				String wurl = wus[i];
				if (StrUtil.isNotNull(wurl)) {
					if (path.startsWith(wurl)) {
						return true;
					}
				}
			}
		}
		ArrayList<String> whiteUrl2 = whiteMap.get("whiteUrl");
		ArrayList<String> whiteParadigm = whiteMap.get("whiteParadigm");
		String ctx = req.getContextPath(); //获取上下文，如/项目名
		uri = uri.substring(ctx.length());
		//url白名单
		if (whiteUrl2.contains(uri)) {
			return true;
		} else {
			int wp = whiteParadigm.size();
			if (uri.length() > 1) {//主要是/造成,如guo/后面有内容,则进入以下方法
				for (int i = 0; i < wp; i++) {
					if ((whiteParadigm.get(i)).contains("*")) {
						String wdir = (whiteParadigm.get(i)).replace("*", "");
						int s = uri.indexOf(wdir);
						if (s == 0) {
							return true;
						}
					} else {
						if (!"".equals(whiteParadigm.get(i))) {
							int s = uri.indexOf(whiteParadigm.get(i));
							if (s == 0) {
								return true;
							}
						}
					}
				}
			}
		}
		return false;
	}

	/**
	 * description:  用户退出操作
	 */
	private String doLogout(HttpServletRequest req, HttpServletResponse resp,
							String tokenCookie, String loginUrl,String defaultSso,RequestContext currentContext) {
		try {
			if (StrUtil.isNull(tokenCookie)) {
				clearCookie("", req, resp, "/");
				return loginUrl;
			}
			String userEname = PassportHelper.getInstance().getCurrCookie(req, "_uc.sso");
			if (StrUtil.isNull(userEname)) {
				userEname = (String) req.getSession().getAttribute("message");
			}

			ResultEntity entity = this
					.checkToken(tokenCookie, req.getLocalAddr(), "logout", userEname);
			if ("true".equals(entity.getResult())) {
				clearCookie(tokenCookie, req, resp, "/");
			}
			if (tokenCookie.indexOf("uni_") == -1) {
				return loginUrl;
			} else {
				String csetUrl = getLonginUrl(req, resp, "",defaultSso);
				return csetUrl;
			}
		} catch (Exception e) {
		}
		return loginUrl;
	}

	/**
	 * description:  清空本地cookie
	 */
	private void clearCookie(String tokenId, HttpServletRequest request,
							 HttpServletResponse response, String path) {
		Cookie[] cookies = request.getCookies();
		try {
			if (null != cookies && cookies.length > 0) {
				for (int i = 0; i < cookies.length; i++) {
					Cookie cookie = new Cookie(cookies[i].getName(), null);
					cookie.setMaxAge(0);
					cookie.setPath("/");
					cookie.setPath(path);//根据你创建cookie的路径进行填写
					response.addCookie(cookie);
				}
			}
		} catch (Exception ex) {
		}
		String isClearSession = ToolsUtil.getInstance().getValueByKey("is_clear_session");
		if (!"0".equals(isClearSession)) {
			Enumeration enumer = request.getSession().getAttributeNames();
			while (enumer.hasMoreElements()) {
				request.getSession().removeAttribute(enumer.nextElement().toString());
			}
			request.getSession().invalidate();
		}
	}

	private String getLonginUrl(HttpServletRequest request, HttpServletResponse response,
								String tokenId,String defaultSso) throws UnsupportedEncodingException {
		String loginUrl = "";
		String customLogin = ToolsUtil.getInstance().getValueByKey("custom_login");
		if (StrUtil.isNotNull(tokenId) && tokenId.indexOf("uni_") > -1) {
			loginUrl = doSsoFiltersForEid(request, response, tokenId, "");
		} else if ("sso".equals(defaultSso)) {
			loginUrl = doSsoFilters(request, response, tokenId, "");
		} else if ("eid".equals(defaultSso)) {
			loginUrl = doSsoFiltersForEid(request, response, tokenId, "");
		} else {
			loginUrl = request.getContextPath() + StrUtil.doNull(customLogin, "/login.jsp");
		}
		return loginUrl;
	}

	/**
	 * description: sso参数组装
	 */
	private String doSsoFilters(HttpServletRequest request, HttpServletResponse response,
								String tokenId, String pkt) throws UnsupportedEncodingException {
		String path = request.getServletPath();
		//要跳转到哪个url
		String gotoURL = request.getParameter("go");
		String csetUrl = request.getScheme() + "://" + request.getServerName() + ":" + request
				.getServerPort() + request.getContextPath();

		if (gotoURL == null) {
			gotoURL = request.getRequestURL().toString();
		}
		String reqParams = "";//customLogin
		if ("T".equals(ToolsUtil.getInstance().getValueByKey("is_url_params"))) {
			reqParams = PassportHelper.getInstance().getReqParams(request, "go");
			reqParams = PassportHelper.getInstance().isNov(reqParams, reqParams, "?" + reqParams);
		}
		String login_url = ToolsUtil.getInstance().getValueByKey("uct_server");
		//是否使用统一登录系统登录 T/F
		String login_pass = ToolsUtil.getInstance().getValueByKey("login.pass");
		ArrayList<String> lst = new ArrayList<String>();
		String sha1Rtn = "";
		if (StrUtil.isNull(tokenId) && StrUtil.isNotNull(pkt)) {//客户端调用
			StringBuffer tokenUrl = new StringBuffer();
			lst.add(login_url);
			lst.add(pkt);
			lst.add(gotoURL + reqParams);
			lst.add("apache");
			lst.add(csetUrl);
			Collections.sort(lst);
			sha1Rtn = PassportHelper.getInstance().SHA1(lst);
			if ("T".equals(login_pass)) {
				//如果cookie为空,则跳转到登录页面
				tokenUrl.append(login_url);
				tokenUrl.append("?");
				tokenUrl.append("tokenId=" + pkt);//设置远程cookie
				tokenUrl.append("&go=" + gotoURL + reqParams);
				tokenUrl.append("&ptlang=" + sha1Rtn);
				tokenUrl.append("&cset=" + csetUrl);
			} else {
				tokenUrl.append(login_url);
			}
			request.removeAttribute("tokenId");
			return tokenUrl.toString();
		}/////客户端调用结束
		String sysEname = ToolsUtil.getInstance().getValueByKey("sysEname");
		//统一登录系统平台回调参数
		String cset = csetUrl + "/cset";
		lst.add(login_url);
		lst.add(sysEname);
		lst.add(cset);
		if (request.getRequestURI().equals(path + "/logout")) {
			lst.add(gotoURL);
		} else {
			lst.add(gotoURL + reqParams);
		}
		lst.add("apache");
		Collections.sort(lst);
		sha1Rtn = PassportHelper.getInstance().SHA1(lst);

		StringBuffer loginUrl = new StringBuffer();
		if ("T".equals(login_pass)) {
			//如果cookie为空,则跳转到登录页面
			loginUrl.append(login_url);
			loginUrl.append("?");
			loginUrl.append("_client=" + sha1Rtn);
			loginUrl.append("&");
			loginUrl.append("sys=" + sysEname);//系统名称
			loginUrl.append("&");
			loginUrl.append("cset=" + URLEncoder.encode(cset, "UTF-8"));//设置远程cookie
			loginUrl.append("&");
			loginUrl.append("go=" + URLEncoder.encode(gotoURL, "UTF-8") + reqParams);
		} else {
			loginUrl.append(login_url);
		}
		return loginUrl.toString();
	}

	/**
	 * description: Eid参数组装
	 */
	private String doSsoFiltersForEid(HttpServletRequest request, HttpServletResponse response,
									  String tokenId, String pkt) throws UnsupportedEncodingException {
		//要跳转到哪个url
		String gotoURL = request.getParameter("go");

		if (gotoURL == null) {
			gotoURL = request.getRequestURL().toString();
		}
		String reqParams = "";//customLogin
		if ("T".equals(ToolsUtil.getInstance().getValueByKey("is_url_params"))) {
			reqParams = PassportHelper.getInstance().getReqParams(request, "go,code");
			reqParams = PassportHelper.getInstance().isNov(reqParams, reqParams, "?" + reqParams);
		}
		String login_url = ToolsUtil.getInstance().getValueByKey("uct_server");
		//是否使用统一登录系统登录 T/F
		String login_pass = ToolsUtil.getInstance().getValueByKey("login.pass");
		String sysEname = ToolsUtil.getInstance().getValueByKey("sysEname");
		ArrayList<String> lst = new ArrayList<String>();
		String sha1Rtn = "";
		if (StrUtil.isNull(tokenId) && StrUtil.isNotNull(pkt)) {//客户端调用
			StringBuffer tokenUrl = new StringBuffer();
			lst.add(login_url);
			lst.add("apache");
			Collections.sort(lst);
			sha1Rtn = PassportHelper.getInstance().SHA1(lst);
			if ("T".equals(login_pass)) {
				//如果cookie为空,则跳转到登录页面
				tokenUrl.append(login_url);
				tokenUrl.append("?_client=" + sha1Rtn);
				tokenUrl.append("&tokenId=" + pkt);//设置远程cookie
				tokenUrl.append("&clientid=" + sysEname);//系统名称
				tokenUrl.append("&response_type=" + "code");
				tokenUrl.append("&scope=" + "openid");
				tokenUrl.append("&go=" + gotoURL + reqParams);
			} else {
				tokenUrl.append(login_url);
			}
			request.removeAttribute("tokenId");
			return tokenUrl.toString();
		}/////客户端调用结束

		//统一登录系统平台回调参数
		lst.add(login_url);
		lst.add(sysEname);
		lst.add("apache");
		Collections.sort(lst);
		sha1Rtn = PassportHelper.getInstance().SHA1(lst);

		StringBuffer loginUrl = new StringBuffer();
		if ("T".equals(login_pass)) {
			//如果cookie为空,则跳转到登录页面
			loginUrl.append(login_url);
			loginUrl.append("?_client=" + sha1Rtn);
			loginUrl.append("&clientid=" + sysEname);//系统名称
			loginUrl.append("&response_type=" + "code");
			loginUrl.append("&scope=" + "openid");
			loginUrl.append("&go=" + URLEncoder.encode(gotoURL, "UTF-8") + reqParams);
		} else {
			loginUrl.append(login_url);
		}
		return loginUrl.toString();
	}

	/**
	 * description:  获取accessToken
	 */
	private String getAccessToken(String code, HttpServletRequest req, HttpServletResponse resp) {
		String tokenId = "";
		String login_url = ToolsUtil.getInstance().getValueByKey("uct_server");
		try {
			String clientEname = ToolsUtil.getInstance().getValueByKey("sysEname");
			String url = login_url + "passport/outside/access_token";
			NameValuePair[] parasInput = new NameValuePair[3];
			parasInput[0] = new BasicNameValuePair("code", code);
			parasInput[1] = new BasicNameValuePair("clientid", clientEname);
			parasInput[2] = new BasicNameValuePair("grant_type", "authorization_code");
			String jsonStr = "";
			if (url.startsWith("https:")) {
				jsonStr = PostHttps.newInstance().getHttpsForStl(url, parasInput, "GET");
			} else {
				jsonStr = PostHttps.newInstance().getHttp(url, parasInput, "GET");
			}
			if (StrUtil.isNull(jsonStr))
				return "";
			JSONObject json = JSONObject.fromObject(jsonStr);
			if ("200".equals(json.get("state"))) {
				String cookieName = ToolsUtil.getInstance().getValueByKey("cookieName");
				String idToken = json.getString("id_token");
				Claims claims = JwtUtil.newInstance().uniParseJWT(idToken);
				if (null == claims) {
					return "";
				}
				String aud = claims.getAudience();
				if (!aud.contains(clientEname))
					return "";
				JSONObject ujson = JSONObject.fromObject(claims.get("u_info"));
				String userEname = ujson.getString("userEname");
				LoginUser loginUser = PBOSSOTools
						.getLoginUserFromUserCenter(userEname, "uni_" + code);
				HttpSession session = req.getSession();
				session.setAttribute("loginUser", loginUser);
				session.setAttribute("id_token", idToken);
				session.setAttribute("access_token", claims.get("u_info"));
				session.setAttribute("token_code", code);
				Cookie ticket = new Cookie(cookieName, "uni_" + code);
				ticket.setPath("/");
				ticket.setMaxAge(-1);
				resp.addCookie(ticket);
				Cookie ucssoCKey = new Cookie("_uc.sso", userEname);
				ucssoCKey.setPath("/");
				ucssoCKey.setMaxAge(-1);
				resp.addCookie(ucssoCKey);
				tokenId = "uni_" + code;
			}
		} catch (Exception e) {
		}
		return tokenId;
	}

	/**
	 * description: 验证tokenId是否有效
	 */
	private boolean auditTokenAndSso(String tokenId, HttpServletRequest req,
									 HttpServletResponse resp) {
		if (StrUtil.isNull(tokenId)) {
			return false;
		}
		String ss[] = tokenId.split("\\|");
		tokenId = ss[0];
		if (tokenId.indexOf("uni_") > -1) {
			String accessToken = String.valueOf(req.getSession().getAttribute("id_token"));
			if (StrUtil.isNull(accessToken)) {
				getAccessToken(tokenId.replace("uni_", ""), req, resp);
				accessToken = String.valueOf(req.getSession().getAttribute("id_token"));
			}
			if (StrUtil.isNull(accessToken)) {
				return false;
			}
			Claims claims = JwtUtil.newInstance().uniParseJWT(accessToken);
			if (null == claims)
				return false;
			String sysEname = ToolsUtil.getInstance().getValueByKey("sysEname");
			String aud = claims.getAudience();
			if (!aud.contains(sysEname))
				return false;
			long expiresOut = Long.valueOf(String.valueOf(claims.get(Claims.EXPIRATION)));
			long nowTime = System.currentTimeMillis() / 1000;
			if (nowTime > expiresOut) {
				return false;
			}
			return true;
		}
		String cookieName = ToolsUtil.getInstance().getValueByKey("cookieName");
		long nowTimeLong = System.currentTimeMillis();
		Object obj = req.getSession().getAttribute("time_" + tokenId);
		if (null != obj) {//五分钟内不重复验证
			long upTimeLong = Long.valueOf(StrUtil.doNull(String.valueOf(obj), nowTimeLong + ""));
			if ((nowTimeLong - upTimeLong) < 5 * 60 * 1000) {
				return true;
			}
		}

		ResultEntity entity = this.checkToken(tokenId, req.getLocalAddr(), "checkToken", "");
		String str = String.valueOf(entity.getEntity());
		if (StrUtil.isNotNull(str) && tokenId.equalsIgnoreCase(str)) {
			Cookie ticket = new Cookie(cookieName, tokenId);
			ticket.setPath("/");
			ticket.setMaxAge(-1);
			Cookie ucssoCKey = new Cookie("_uc.sso", entity.getMessage());
			ucssoCKey.setPath("/");
			ucssoCKey.setMaxAge(-1);
			resp.addCookie(ticket);
			resp.addCookie(ucssoCKey);
			req.getSession().setAttribute("time_" + tokenId, nowTimeLong);
			return true;
		}
		return false;
	}

	/**
	 * description:  验证token是否有效/退出系统
	 * methodKey:checkToken/logout
	 */
	private ResultEntity checkToken(String tokenId, String ip, String methodKey,
									String userEname) {
		ResultEntity entity = null;
		String sysEname = ToolsUtil.getInstance().getValueByKey("sysEname");
		if ("https".equalsIgnoreCase(ToolsUtil.getInstance().getValueByKey("req_type"))) {
			String httpsUrl = ToolsUtil.getInstance().getValueByKey("req_url");
			NameValuePair[] parasInput = new NameValuePair[4];
			parasInput[0] = new BasicNameValuePair("sysAccreditip", ip);
			parasInput[1] = new BasicNameValuePair("tokenId", tokenId);
			parasInput[2] = new BasicNameValuePair("sysEname", sysEname);
			parasInput[3] = new BasicNameValuePair("userEname", userEname);
			String jsonStr = PostHttps.newInstance().getHttpsForStl(httpsUrl, parasInput, "GET");
			entity = LoadRpcService.service().xmlToBean(jsonStr, ResultEntity.class);
		} else {
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("sysAccreditip", ip);
			param.put("tokenId", tokenId);
			param.put("sysEname", sysEname);
			param.put("userEname", userEname);
			entity = LoadRpcService.service()
					.doServiceClient("ssoService", methodKey, param, getInterfaceRegister());
		}
		return entity;
	}

	/**
	 * description:  设置本地cookie
	 *
	 * @param req
	 * @param resp
	 */
	private String setCookie(HttpServletRequest req, HttpServletResponse resp) {
		String token = req.getParameter("ticket");
		String ucsso = req.getParameter("ucsso");
		String gotoURL = req.getParameter("go");
		String cookieName = ToolsUtil.getInstance().getValueByKey("cookieName");
		if (StrUtil.isNotNull(gotoURL)) {
			gotoURL = gotoURL.replaceAll("\\|", "&");
			if (gotoURL.indexOf("token_code") == -1 && StrUtil.isNotNull(token) && StrUtil
					.isNotNull(ucsso)) {
				Cookie ticket = new Cookie(cookieName, token);
				ticket.setPath("/");
				ticket.setMaxAge(-1);

				Cookie ucssoCKey = new Cookie("_uc.sso", ucsso);
				ucssoCKey.setPath("/");
				ucssoCKey.setMaxAge(-1);
				resp.addCookie(ticket);
				resp.addCookie(ucssoCKey);
				if (gotoURL.indexOf("?tokenId=") != -1) {
					gotoURL = gotoURL.replaceAll("tokenId=" + token + "&", "");
				} else {
					gotoURL = gotoURL.replaceAll("&tokenId=" + token, "");
				}
			}
			return gotoURL;
		}
		return "";
	}

	private InterfaceRegister getInterfaceRegister() {
		InterfaceRegister register = new InterfaceRegister();
		String type = ToolsUtil.getInstance().getValueByKey("req_type");
		String ip = ToolsUtil.getInstance().getValueByKey("check_url");
		if ("https".equalsIgnoreCase(type)) {
			String ssoPath = ToolsUtil.getInstance().getValueByKey("req_url");
			register.setAddress(ssoPath);
			register.setCallType("https");
		} else if ("socket".equals(type) || StrUtil.isNotNull(ip)) {
			String port = ToolsUtil.getInstance().getValueByKey("check_port");
			register.setAddress(ip);
			register.setPort(port);
			register.setCallType("socket");
		} else {
			String ssoPath = ToolsUtil.getInstance().getValueByKey("req_url");
			register.setAddress(ssoPath);
			register.setCallType("http");
		}
		return register;
	}
}
